<script setup>
import { Delete, Edit } from '@element-plus/icons-vue';
import { ref } from 'vue'
import { onMounted } from 'vue';
import { getAllDept, addApi, queryByIdApi, editApi, deleteByIdApi } from '@/api/dept';
import { ElMessage, ElMessageBox } from 'element-plus'




const deptList = ref([])
const dialogVisible = ref(false)
const dept = ref({ name: '' })
const formTitle = ref('')
const deptFormref = ref()
const rules = ref({
  name: [
    { required: true, message: '请输入部门名称', trigger: 'blur' },
    { min: 3, max: 5, message: '部门名称的长度需在2-10之间', trigger: 'blur' },
  ],
})

onMounted(() => {
  search()
})

const search = async () => {
  const result = await getAllDept();
  if (result.code) {
    deptList.value = result.data;
  }
}

const addDept = () => {
  dialogVisible.value = true;
  formTitle.value = '新增部门';

  // 清空表单
  dept.value.name = '';

  if (deptFormref.value) {
    //重置表单提醒
    deptFormref.value.resetFields();
  }
}

// 新增部门确认提交
const submitDept = () => {
  //表单校验
  if (!deptFormref.value) { return }
  deptFormref.value.validate(async (valid) => {
    if (valid) {
      //有效验证
      //提交
      let result;
      if (dept.value.id) {
        //id不为空 编辑部门
        result = await editApi(dept.value);
      } else {
        //id为空 新增部门
        result = await addApi(dept.value);
      }

      //检查调用是否成功
      if (result.code) {
        //成功
        //提醒
        ElMessage.success('操作成功');
        //关闭对话框
        dialogVisible.value = false;
        //重新查询
        search();
      } else {
        //失败
        //提醒
        ElMessage.error(result.msg);
        dialogVisible.value = false;
      }
    }
    else {
      //无效验证
      //提醒
      ElMessage.error('请填写正确的部门名称');
    }
  })
}

const eidt = async (id) => {

  const result = await queryByIdApi(id);
  if (result.code) {
    dialogVisible.value = true;
    //查询成功 回显
    dept.value = result.data;
    formTitle.value = '编辑部门';
    //重置表单提醒
    deptFormref.value.resetFields();
  } else {
    //查询失败
    ElMessage.error(result.meg);
  }
}

const deleteDept = async (id) => {
  //消息弹出框
  ElMessageBox.confirm(
    '是否确认删除该部门?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async() => { //确认后执行
      const result = await deleteByIdApi(id);
      if (result.code) {
      //删除成功
      ElMessage.success('删除成功');
      search();
      }else{
      //删除失败
      ElMessage.error(result.msg);
      }
    })
    .catch(() => { //取消后执行
     ElMessage.info('删除已取消');
    })
}
</script>

<template>
  <h1>部门管理</h1>
  <div class="container">
    <el-button type="primary" @click="addDept">+ 新增部门</el-button>
  </div>

  <!-- 数据展示--表格 -->
  <el-table :data="deptList" border style="width: 100%">
    <el-table-column type="index" label="序号" width="100" align="center"></el-table-column>
    <el-table-column prop="name" label="名称" width="200" align="center" />
    <el-table-column prop="createTime" label="创建时间" width="200" align="center" />
    <el-table-column prop="updateTime" label="更新时间" width="180" align="center" />

    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button type="primary" round @click="eidt(scope.row.id)">
          <el-icon>
            <Edit />
          </el-icon>编辑</el-button>
        <el-button type="danger" round @click="deleteDept(scope.row.id)">
          <el-icon>
            <Delete />
          </el-icon>删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- dialog对话框 -->
  <el-dialog v-model="dialogVisible" :title="formTitle" width="500" transition="dialog-bounce">
    <el-form :model="dept" :rules="rules" ref="deptFormref">
      <el-form-item label="部门名称" label-width="80px" prop="name">
        <el-input v-model="dept.name" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitDept">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.container {
  margin: 20px 0px;
}
</style>

<!-- 全局样式，不能使用 scoped -->
<style>
/* Bounce Animation */
.dialog-bounce-enter-active,
.dialog-bounce-leave-active,
.dialog-bounce-enter-active .el-dialog,
.dialog-bounce-leave-active .el-dialog {
  transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.dialog-bounce-enter-from,
.dialog-bounce-leave-to {
  opacity: 0;
}

.dialog-bounce-enter-from .el-dialog,
.dialog-bounce-leave-to .el-dialog {
  transform: scale(0.3) translateY(-50px);
  opacity: 0;
}
</style>